﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace mrp16062010.Uretec
{
    [Serializable()]
    class VeriUret:ISerializable
    {
        public int CalismaId { get; set; }
        public List<Siparis> siparisListUzun;
        public List<Siparis> siparisListDuz;
        public List<Siparis> siparisListKarisim;
        public List<Rota.RotaParcasi> parcaListUzun;
        public List<Rota.RotaParcasi> parcaListDuz;
        public List<Rota.RotaParcasi> parcaListKarisim;

        public VeriUret()
        {

        }

        public VeriUret Uret(int calismaNo)
        {
            VeriUret veri = new VeriUret();
            BilgileriHazirla(veri);
            return veri;
        }

        private void BilgileriHazirla(VeriUret veri)
        {
            Uretec.UrunAgaciUret agacUret = new UrunAgaciUret();

            veri.parcaListDuz = agacUret.ListeUret(new AgacCesidi.Duz(), Ayarlar.Nesne.UrunAdedi);
            Uretec.UrunAgaciUret agacUret2 = new UrunAgaciUret();
            veri.parcaListKarisim = agacUret2.ListeUret(new AgacCesidi.Karisim(), Ayarlar.Nesne.UrunAdedi);
            Uretec.UrunAgaciUret agacUret3 = new UrunAgaciUret();
            veri.parcaListUzun = agacUret3.ListeUret(new AgacCesidi.Uzun(), Ayarlar.Nesne.UrunAdedi);
            
            Uretec.SiparisUret siparisUret = Uretec.SiparisUret.Nesne;
            siparisUret.GunlukSiparisSayisi = Ayarlar.Nesne.GunlukSiparisAraligi;
            siparisUret.SiparisBuyuklugu = Ayarlar.Nesne.SiparisBuyuklugu;
            siparisUret.SiparisTeslimSuresi = Ayarlar.Nesne.TeslimAraligi;
            veri.siparisListDuz = siparisUret.Uret(Ayarlar.Nesne.AnaAralik, Ayarlar.Nesne.CalismaSaatleri, veri.parcaListDuz);
            Random rndParca = new Random();
            veri.siparisListKarisim = new List<Siparis>();
            veri.siparisListUzun = new List<Siparis>();
       
            foreach (Siparis siparis in veri.siparisListDuz)
            {
                int sayiParca = rndParca.Next(0, veri.parcaListKarisim.Count - 1);
                Rota.RotaParcasi secilenParca = veri.parcaListKarisim[sayiParca];
                Siparis yeniSiparis = siparis.Kopyala();
                yeniSiparis.Rota = secilenParca;
                veri.siparisListKarisim.Add(yeniSiparis);
            }

            foreach (Siparis siparis in veri.siparisListDuz)
            {
                int sayiParca = rndParca.Next(0, veri.parcaListUzun.Count - 1);
                Rota.RotaParcasi secilenParca = veri.parcaListUzun[sayiParca];
                Siparis yeniSiparis = siparis.Kopyala();
                yeniSiparis.Rota = secilenParca;
                veri.siparisListUzun.Add(yeniSiparis);
            }
        }

        public List<VeriUret> VeriListeUret()
        {
            List<VeriUret> liste = new List<VeriUret>();
            for (int i = 0; i < Ayarlar.Nesne.SimilasyonTekrarSayisi; i++)
            {
                liste.Add(Uret(i+1));
            }
            return liste;
        }

        public VeriUret(SerializationInfo info, StreamingContext ctxt)
        {
            CalismaId = (int)info.GetValue("CalismaId", typeof(int));
            parcaListDuz = (List<Rota.RotaParcasi>)info.GetValue("parcaListDuz", typeof(List<Rota.RotaParcasi>));
            parcaListKarisim = (List<Rota.RotaParcasi>)info.GetValue("parcaListKarisim", typeof(List<Rota.RotaParcasi>));
            parcaListUzun = (List<Rota.RotaParcasi>)info.GetValue("parcaListUzun", typeof(List<Rota.RotaParcasi>));
            siparisListDuz = (List<Siparis>)info.GetValue("siparisListDuz", typeof(List<Siparis>));
            siparisListKarisim = (List<Siparis>)info.GetValue("siparisListKarisim", typeof(List<Siparis>));
            siparisListUzun = (List<Siparis>)info.GetValue("siparisListUzun", typeof(List<Siparis>));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("CalismaId", CalismaId);
            info.AddValue("parcaListDuz", parcaListDuz);
            info.AddValue("parcaListKarisim", parcaListKarisim);
            info.AddValue("parcaListUzun", parcaListUzun);
            info.AddValue("siparisListDuz", siparisListDuz);
            info.AddValue("siparisListKarisim", siparisListKarisim);
            info.AddValue("siparisListUzun", siparisListUzun);
        }

        public static List<Uretec.VeriUret> Oku()
        {
            List<Uretec.VeriUret> veriler2 = Serialize.FileSerialization.FileDeSerialize<List<Uretec.VeriUret>>("C:\\serialized.xml");

            return veriler2;
        }

        public void Duzelt()
        {
            Uretec.UrunAgaciUret agacUret = new UrunAgaciUret();
            this.parcaListKarisim = agacUret.ListeUret(new AgacCesidi.Karisim(), Ayarlar.Nesne.UrunAdedi);
            
            Random rndParca = new Random();
            this.siparisListKarisim.Clear();
            foreach (Siparis siparis in this.siparisListDuz)
            {
                int sayiParca = rndParca.Next(0, this.parcaListKarisim.Count - 1);
                Rota.RotaParcasi secilenParca = this.parcaListKarisim[sayiParca];
                Siparis yeniSiparis = siparis.Kopyala();
                yeniSiparis.Rota = secilenParca;
                this.siparisListKarisim.Add(yeniSiparis);
            }
        }
    }
}
